package chuangyi.com.org.DOMIHome.Utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.databinding.ObservableArrayList;
import android.os.Bundle;
import android.widget.Toast;

import com.solidfire.gson.internal.LinkedTreeMap;
import com.solidfire.gson.reflect.TypeToken;

import org.json.JSONObject;

import java.lang.reflect.Type;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import chuangyi.com.org.DOMIHome.DBManager;
import chuangyi.com.org.DOMIHome.HomeActivity;
import chuangyi.com.org.DOMIHome.LoadingDialog;
import chuangyi.com.org.DOMIHome.LoginActivity;
import chuangyi.com.org.ViewModel.AdviceInfo;
import chuangyi.com.org.ViewModel.AdviceItem;
import chuangyi.com.org.ViewModel.ArticleEditModel;
import chuangyi.com.org.ViewModel.ContextObservable;
import chuangyi.com.org.ViewModel.EditReward;
import chuangyi.com.org.ViewModel.MenuItem;
import chuangyi.com.org.ViewModel.ParagraphItem;
import chuangyi.com.org.ViewModel.User;
import chuangyi.com.org.ViewModel.UserInfoModel;
import chuangyi.com.org.ViewModel.VideoDetail;
import chuangyi.com.org.ViewModel.VideoItem;

/**
 * Created by zxh on 16/10/29.
 */
public class WebApi {

    /**
     * 用户登录
     *
     * @param context   当前调用接口的Activity上下文
     * @param phone
     * @param validCode
     * @return
     */
    public static User login(Context context, String phone, String validCode) {
        Map<String, String> parameters = new HashMap<String, String>();
        parameters.put("phone", phone);
        parameters.put("code", validCode);
        RequestResult result = HttpUtil.get("/MobileJson/Index/login", parameters);
        return ParseResultFromInfo(context, result, User.class);
    }

    /**
     * 发送验证码
     *
     * @param context 当前调用接口的Activity上下文
     * @param phone
     * @return
     */
    public static boolean SendSMS(Context context, String phone) {
        Map<String, String> parameters = new HashMap<String, String>();
        parameters.put("phone", phone);
        RequestResult result = HttpUtil.get("/MobileJson/Index/sendsms", parameters);
        return validResult(context, result);
    }

    /**
     * 获取菜单列表
     *
     * @param context 当前调用接口的Activity上下文
     * @return
     */
    public static ArrayList getMenus(Context context) {
        Map<String, String> parameters = getCommonParameters();
        RequestResult result = HttpUtil.get("/MobileJson/Index/getMenuList", parameters);
        return ParseResultFromList(context, result);
    }

    /**
     * 获取首页视频列表
     *
     * @param context 当前调用接口的Activity上下文
     * @return
     */
    public static ArrayList getIndexVideos(Context context) {
        Map<String, String> parameters = getCommonParameters();
        RequestResult result = HttpUtil.get("/MobileJson/Index/IndexVideoList", parameters);
        return ParseResultFromList(context, result);
    }

    /**
     * 获取首页免费视频列表
     *
     * @param context 当前调用接口的Activity上下文
     * @return
     */
    public static ArrayList getFreeVideos(Context context) {
        Map<String, String> parameters = getCommonParameters();
        RequestResult result = HttpUtil.get("/MobileJson/Index/FreeVideoList", parameters);
        return ParseResultFromList(context, result);
    }

    /**
     * 获取首页免费视频列表
     *
     * @param context  当前调用接口的Activity上下文
     * @param menuItem 点击的菜单项
     * @return
     */
    public static ArrayList getVideoListByMenu(Context context, MenuItem menuItem) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("code", menuItem.getCode());
        RequestResult result = HttpUtil.get("/MobileJson/Index/getVideoListByMenu", parameters);
        return ParseResultFromList(context, result);
    }

    /**
     * 获取相关视频列表
     *
     * @param context 当前调用接口的Activity上下文
     * @param videoId 视频Id
     * @return
     */
    public static ArrayList getRelatedVideos(Context context, int videoId) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("videoid", String.valueOf(videoId));
        RequestResult result = HttpUtil.get("/MobileJson/Index/RelatedVideoList", parameters);
        return ParseResultFromList(context, result);
    }

    /**
     * 获取评论列表
     *
     * @param context     当前调用接口的Activity上下文
     * @param commentType 评论类型（w:文章，v：视频）
     * @param contentId   文章或视频Id
     * @return
     */
    public static ArrayList getCommentList(Context context, String commentType, int contentId) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("number", String.valueOf(contentId));
        parameters.put("commenttype", commentType);
        RequestResult result = HttpUtil.get("/MobileJson/Index/CommentList", parameters);
        return ParseResultFromList(context, result);
    }


    public static boolean deleteExpertAsk(Context context, int adviceId) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("consultid", String.valueOf(adviceId));
        RequestResult result = HttpUtil.get("/MobileJson/Index/delExpertask", parameters);
        return validResult(context, result);
    }

    public static boolean allowAction(Context context) {
        Map<String, String> parameters = getCommonParameters();
        RequestResult result = HttpUtil.get("/MobileJson/Index/userinfocheck", parameters);
        return validResult(context, result);
    }

    /**
     * 用户中心
     *
     * @param context
     * @return
     */
    public static UserInfoModel getUserCenterInfo(Context context) {
        Map<String, String> parameters = getCommonParameters();
        RequestResult result = HttpUtil.get("/MobileJson/Index/Ucenter", parameters);

        LinkedTreeMap map = ParseResultFromInfo(context, result, LinkedTreeMap.class);
        UserInfoModel user = new UserInfoModel(context);
        if (map != null) {
            user.setAvatar((String) map.get("avatar"));
            user.setCompany((String) map.get("company"));
            user.setExpertflag(Utils.getBooleanValue(map.get("expertflag")));
            user.setNickname((String) map.get("nickname"));
            user.setWdfs(Utils.getIntValue(map.get("wdfs")));
            user.setWdgz(Utils.getIntValue(map.get("wdgz")));
            user.setWdxx(Utils.getIntValue(map.get("wdxx")));
        }
        return user;
    }

    /**
     * 获取职位列表
     *
     * @param context
     * @return
     */
    public static List<String> getJobList(Context context) {
        Map<String, String> parameters = getCommonParameters();
        RequestResult result = HttpUtil.get("/MobileJson/Index/JobList", parameters);
        ArrayList<String> list = new ArrayList<String>();
        if (validResult(context, result)) {
            for (Object item : result.getList()) {
                LinkedTreeMap map = (LinkedTreeMap) item;
                list.add((String) map.get("title"));
            }
        }
        return list;
    }

    /**
     * 获取行业列表
     *
     * @param context
     * @return
     */
    public static List<String> getIndustryList(Context context) {
        Map<String, String> parameters = getCommonParameters();
        RequestResult result = HttpUtil.get("/MobileJson/Index/IndustryList", parameters);
        ArrayList<String> list = new ArrayList<String>();
        if (validResult(context, result)) {
            for (Object item : result.getList()) {
                LinkedTreeMap map = (LinkedTreeMap) item;
                list.add((String) map.get("title"));
            }
        }
        return list;
    }

    public static boolean saveUserInfo(Context context, UserInfoModel userInfo) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("avatar", userInfo.getAvatar());
        parameters.put("nickname", userInfo.getNickname());
        parameters.put("realname", userInfo.getRealname());
        parameters.put("company", userInfo.getCompany());
        parameters.put("job", userInfo.getJob());
        parameters.put("zone", userInfo.getZone());
        parameters.put("industry", userInfo.getIndustry());
        parameters.put("phone", userInfo.getPhone());
        RequestResult result = HttpUtil.get("/MobileJson/Index/updateUserInfo", parameters);
        return validResult(context, result);
    }

    /**
     * 获取更多用户信息
     *
     * @param context
     * @return
     */
    public static void getMoreUserInfo(Context context, UserInfoModel user) {
        Map<String, String> parameters = getCommonParameters();
        RequestResult result = HttpUtil.get("/MobileJson/Index/getUserInfo", parameters);

        LinkedTreeMap map = ParseResultFromInfo(context, result, LinkedTreeMap.class);
        if (map != null) {
            user.setAvatar((String) map.get("avatar"));
            user.setNickname((String) map.get("nickname"));
            user.setRealname((String) map.get("realname"));
            user.setJob((String) map.get("job"));
            user.setIndustry((String) map.get("industry"));
            user.setZone((String) map.get("zone"));
//            user.setPhone((String) map.get("phone"));
            user.setCompany((String) map.get("company"));
        }
    }

    public static VideoDetail getVideoDetail(Context context, int videoId) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("videoid", String.valueOf(videoId));
        RequestResult result = HttpUtil.get("/MobileJson/Index/VideoDetail", parameters);
//        videoname：标题,videodesc:视频介绍,cover:视频封面,videopath:视频路径,freeflag：是否收费,price：单价,jifen:所需积分，
//        videotype:视频类型,speaker:讲师,uploaddate:上传时间,notespath:课件路径,videolike:点赞数,
//                collect:收藏数,videoshare:分享数
        LinkedTreeMap map = ParseResultFromInfo(context, result, LinkedTreeMap.class);

        VideoDetail detail = new VideoDetail(context);
        if (map != null) {
            VideoItem videoItem = new VideoItem(context, 0);
            videoItem.setVideoid(Utils.getIntValue(map.get("videoid")));
            videoItem.setVideotype((String) map.get("videotype"));
            videoItem.setVideoname((String) map.get("videoname"));
            videoItem.setVideodesc((String) map.get("videodesc"));
            videoItem.setCover((String) map.get("cover"));
            videoItem.setSpeaker((String) map.get("speaker"));
            videoItem.setVideopath((String) map.get("videopath"));
            videoItem.setFreeflag(Utils.getIntValue(map.get("freeflag")));
            videoItem.setPrice(Utils.getDoubleValue(map.get("price")));
            videoItem.setUploadate(Utils.getDateFromTimestamp(map.get("uploaddate")));
            videoItem.setUid(Utils.getIntValue(map.get("userid")));

            detail.setAttentionFlag(Utils.getBooleanValue(map.get("attentionflag")));
            detail.setAvatar((String) map.get("avatar"));
            detail.setNotesimgpath((String) map.get("notesimgpath"));
            detail.setNotespath((String) map.get("notespath"));
            detail.setVideo(videoItem);
            detail.setVideolike(Utils.getIntValue(map.get("videolike")));
            detail.setCollect(Utils.getIntValue(map.get("collect")));
            detail.setJifen(Utils.getIntValue(map.get("jifen")));

            detail.setLikeflag(Utils.getBooleanValue(map.get("likeflag")));
            detail.setCollectflag(Utils.getBooleanValue(map.get("collectflag")));
            detail.setUploaddate(Utils.getDateFromTimestamp(map.get("uploaddate")));
            detail.setVideoshare(Utils.getIntValue(map.get("videoshare")));
        }
        return detail;
    }

    /**
     * 收藏视频或文章
     *
     * @param context     当前调用接口的Activity上下文
     * @param collectType 收藏类型（w:文章，v：视频）
     * @param contentId   文章或视频Id
     * @return
     */
    public static int addCollection(Context context, String collectType, int contentId) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("number", String.valueOf(contentId));
        parameters.put("collecttype", collectType);
        RequestResult result = HttpUtil.get("/MobileJson/Index/addCollection", parameters);
        LinkedTreeMap map = ParseResultFromInfo(context, result, LinkedTreeMap.class);
        return map == null ? -1 : Integer.valueOf((String) map.get("coun"));
    }

    /**
     * 取消收藏视频或文章
     *
     * @param context     当前调用接口的Activity上下文
     * @param collectType 收藏类型（w:文章，v：视频）
     * @param contentId   文章或视频Id
     * @return
     */
    public static int removeCollection(Context context, String collectType, int contentId) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("number", String.valueOf(contentId));
        parameters.put("collecttype", collectType);
        RequestResult result = HttpUtil.get("/MobileJson/Index/unCollection", parameters);
        LinkedTreeMap map = ParseResultFromInfo(context, result, LinkedTreeMap.class);
        return map == null ? -1 : Integer.valueOf((String) map.get("coun"));
    }

    /**
     * 视频点赞
     *
     * @param context 当前调用接口的Activity上下文
     * @param videoId 视频Id
     * @return
     */
    public static int addLikeVideo(Context context, int videoId) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("videoid", String.valueOf(videoId));
        RequestResult result = HttpUtil.get("/MobileJson/Index/VideoLike", parameters);
        LinkedTreeMap map = ParseResultFromInfo(context, result, LinkedTreeMap.class);
        return map == null ? -1 : Integer.valueOf((String) map.get("coun"));
    }

    /**
     * 评论点赞
     *
     * @param context   当前调用接口的Activity上下文
     * @param commentId 评论Id
     * @return
     */
    public static int addLikeComment(Context context, int commentId) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("commentid", String.valueOf(commentId));
        RequestResult result = HttpUtil.get("/MobileJson/Index/CommentLike", parameters);
        LinkedTreeMap map = ParseResultFromInfo(context, result, LinkedTreeMap.class);
        return map == null ? -1 : Integer.valueOf((String) map.get("coun"));
    }

    /**
     * 获取我的消息列表
     *
     * @param context
     * @param type
     * @return
     */
    public static ArrayList getMyNews(Context context, int type) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("type", String.valueOf(type));

        RequestResult result = HttpUtil.get("/MobileJson/Index/MyNews", parameters);
        return ParseResultFromList(context, result);
    }

    /**
     * 读消息
     *
     * @param context
     * @param id
     * @return
     */
    public static boolean readNews(Context context, int id) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("nid", String.valueOf(id));
        RequestResult result = HttpUtil.get("/MobileJson/Index/MyNewsInfo", parameters);
        return validResult(context, result);
    }

    /**
     * 获取账户余额
     *
     * @param context
     * @return
     */
    public static double getBalance(Context context) {
        Map<String, String> parameters = getCommonParameters();
        RequestResult result = HttpUtil.get("/MobileJson/Index/getBalance", parameters);
        LinkedTreeMap map = ParseResultFromInfo(context, result, LinkedTreeMap.class);
        if (map != null) {
            String value = (String) map.get("balance");
            try {
                return Double.valueOf(value);
            } catch (Exception ex) {
                return 0;
            }
        }
        return 0;
    }

    public static ArrayList getBanner(Context context) {
        Map<String, String> parameters = getCommonParameters();
        RequestResult result = HttpUtil.get("/MobileJson/Index/GetBanner", parameters);
        return ParseResultFromList(context, result);
    }

    /**
     * 获取账户余额变动明细
     *
     * @param context
     * @return
     */
    public static ArrayList getBalanceDetail(Context context, int page) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("page", String.valueOf(page));
        RequestResult result = HttpUtil.get("/MobileJson/Index/getBalanceDetail", parameters);
        return ParseResultFromList(context, result);
    }

    /**
     * 获取用户积分
     *
     * @param context
     * @return
     */
    public static double getScore(Context context) {
        Map<String, String> parameters = getCommonParameters();

        RequestResult result = HttpUtil.get("/MobileJson/Index/getIntegral", parameters);
        LinkedTreeMap map = ParseResultFromInfo(context, result, LinkedTreeMap.class);
        if (map != null) {
            String value = (String) map.get("balance");
            try {
                return Double.valueOf(value);
            } catch (Exception ex) {
                return 0;
            }
        }
        return 0;
    }

    /**
     * 获取积分变动明细
     *
     * @param context
     * @return
     */
    public static ArrayList getScoreDetail(Context context, int page) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("page", String.valueOf(page));
        RequestResult result = HttpUtil.get("/MobileJson/Index/getIntegralDetail", parameters);
        return ParseResultFromList(context, result);
    }

    /**
     * 获取收藏的文章列表
     *
     * @param page
     * @return
     */
    public static ArrayList getCollectedArticle(Context context, int page) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("page", String.valueOf(page));
        RequestResult result = HttpUtil.get("/MobileJson/Index/myCollectDocument", parameters);
        return ParseResultFromList(context, result);
    }

    /**
     * 获取收藏的视频列表
     *
     * @param page
     * @return
     */
    public static ArrayList getCollectedVideo(Context context, int page) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("page", String.valueOf(page));
        RequestResult result = HttpUtil.get("/MobileJson/Index/myCollectVideo", parameters);
        return ParseResultFromList(context, result);
    }

    /**
     * 获取我上传的文档
     *
     * @param context
     * @param page
     * @return
     */
    public static ArrayList getMyDocument(Context context, int page) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("page", String.valueOf(page));
        RequestResult result = HttpUtil.get("/MobileJson/Index/MyDocument", parameters);
        return ParseResultFromList(context, result);
    }

    /**
     * 删除文章
     *
     * @param context
     * @param articleId
     * @return
     */
    public static boolean deleteDocument(Context context, int articleId) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("articleid", String.valueOf(articleId));

        RequestResult result = HttpUtil.get("/MobileJson/Index/DelDocument", parameters);
        return validResult(context, result);
    }

    /**
     * 获取文章资讯列表
     *
     * @param context
     * @param page
     * @return
     */
    public static ArrayList getArticleList(Context context, int page) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("page", String.valueOf(page));
        RequestResult result = HttpUtil.get("/MobileJson/Index/DocumentList", parameters);
        return ParseResultFromList(context, result);
    }

    /**
     * 获取提问/回答列表
     *
     * @param context
     * @param page
     * @return
     */
    public static LinkedTreeMap getQuestionList(Context context, String type, String sort, int page) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("type", type);
        parameters.put("order", sort);
        parameters.put("page", String.valueOf(page));
        RequestResult result = HttpUtil.get("/MobileJson/Index/askList", parameters);
        return ParseResultFromInfo(context, result, LinkedTreeMap.class);
    }

    /**
     * 获取专家咨询列表
     *
     * @param context
     * @param page
     * @return
     */
    public static ArrayList getExpertList(Context context, String type, String sort, int page) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("type", type);
        parameters.put("order", sort);
        parameters.put("page", String.valueOf(page));
        RequestResult result = HttpUtil.get("/MobileJson/Index/expertList", parameters);
        return ParseResultFromList(context, result);
    }

    /**
     * 获取我的提问列表
     *
     * @param context
     * @return
     */
    public static ArrayList getMyQuestion(Context context, String type) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("type", type);
        RequestResult result = HttpUtil.get("/MobileJson/Index/myAsk", parameters);
        return ParseResultFromList(context, result);
    }

    /**
     * 获取我的悬赏列表
     *
     * @param context
     * @return
     */
    public static ArrayList getMyReward(Context context, String type) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("type", type);
        RequestResult result = HttpUtil.get("/MobileJson/Index/myReward", parameters);
        return ParseResultFromList(context, result);
    }

    /**
     * 添加段落
     *
     * @param context
     * @param paragraph
     * @return
     */
    public static LinkedTreeMap addParagraph(Context context, Integer id, String label, String title, ParagraphItem paragraph, boolean isArticle) {
        Map<String, String> parameters = getCommonParameters();
        if (id != null) {
            if (isArticle) {
                parameters.put("docid", String.valueOf(id));
                parameters.put("status", "0");
            } else {
                parameters.put("questionid", String.valueOf(id));
            }
        }
        parameters.put("lable", label);
        parameters.put("title", title);
        parameters.put("content", paragraph.getContent());
        parameters.put("contenttype", String.valueOf(paragraph.getContentType()));
        if (isArticle) {
            RequestResult result = HttpUtil.get("/MobileJson/Article/addSection", parameters);
            return ParseResultFromInfo(context, result, LinkedTreeMap.class);
        } else {
            RequestResult result = HttpUtil.get("/MobileJson/Index/addSection", parameters);
            return ParseResultFromInfo(context, result, LinkedTreeMap.class);
        }
    }

    /**
     * 发布文章
     *
     * @param context
     * @param id
     * @return
     */
    public static boolean publishParagraph(Context context, int id) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("docid", String.valueOf(id));
        RequestResult result = HttpUtil.get("/MobileJson/Article/DoSubmit", parameters);
        return validResult(context, result);
    }

    /**
     * 修改段落
     *
     * @param context
     * @param paragraphId
     * @param content
     * @return
     */
    public static boolean updateParagraph(Context context, int paragraphId, String content, boolean isArticle) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("id", String.valueOf(paragraphId));
        parameters.put("content", content);
        if (isArticle) {
            RequestResult result = HttpUtil.get("/MobileJson/Article/updSection", parameters);
            return validResult(context, result);
        } else {
            RequestResult result = HttpUtil.get("/MobileJson/Index/updSection", parameters);
            return validResult(context, result);
        }
    }

    public static boolean upMoveParagraph(Context context, int id, int paragraphId, boolean isArticle) {
        Map<String, String> parameters = getCommonParameters();
        if (isArticle) {
            parameters.put("docid", String.valueOf(id));
        } else {
            parameters.put("questionid", String.valueOf(id));
        }
        parameters.put("id", String.valueOf(paragraphId));
        if (isArticle) {
            RequestResult result = HttpUtil.get("/MobileJson/Article/upMovSection", parameters);
            return validResult(context, result);
        } else {
            RequestResult result = HttpUtil.get("/MobileJson/Index/upMovSection", parameters);
            return validResult(context, result);
        }
    }

    public static boolean downMoveParagraph(Context context, int id, int paragraphId, boolean isArticle) {
        Map<String, String> parameters = getCommonParameters();
        if (isArticle) {
            parameters.put("docid", String.valueOf(id));
        } else {
            parameters.put("questionid", String.valueOf(id));
        }
        parameters.put("id", String.valueOf(paragraphId));
        if (isArticle) {
            RequestResult result = HttpUtil.get("/MobileJson/Article/downMovSection", parameters);
            return validResult(context, result);
        } else {
            RequestResult result = HttpUtil.get("/MobileJson/Index/downMovSection", parameters);
            return validResult(context, result);
        }
    }

    /**
     * 发布悬赏
     *
     * @param context
     * @param questionId
     * @param startDate
     * @param endDate
     * @param money
     * @param payType
     * @return
     */
    public static String submitAsk(Context context, int questionId, java.util.Date startDate,
                                   java.util.Date endDate, double money, String payType) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("questionid", String.valueOf(questionId));
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        if (startDate != null) {
            parameters.put("startdate", format.format(startDate));
        }
        if (endDate != null) {
            parameters.put("enddate", format.format(endDate));
        }
        parameters.put("money", String.valueOf(money));
        parameters.put("paytype", payType);
        RequestResult result = HttpUtil.get("/MobileJson/Index/submitAsk", parameters);
        if ("payorder".equals(result.getCode())) {
            return result.getInfo();
        } else if (validResult(context, result)) {
            return result.getInfo();
        } else {
            return null;
        }
    }

    public static boolean doJudjer(Context context, int expertId, int consultId, String content, float star) {
//        http://test.wldsart.com/MobileJson/Index/dojudjer
        Map<String, String> parameters = getCommonParameters();
        parameters.put("judgerank", String.format("%.2f", star));
        parameters.put("consultid", String.valueOf(consultId));
        parameters.put("expertid", String.valueOf(expertId));
        parameters.put("judgercontent", content);
        RequestResult result = HttpUtil.get("/MobileJson/Index/dojudjer", parameters);
        return validResult(context, result);
    }

    /**
     * 获取悬赏详情
     *
     * @param context
     * @param questionId
     * @return
     */
    public static LinkedTreeMap getRewardDetail(Context context, int questionId) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("questionid", String.valueOf(questionId));
        RequestResult result = HttpUtil.get("/MobileJson/Index/rewardDetail", parameters);
        return ParseResultFromInfo(context, result, LinkedTreeMap.class);
    }

    /**
     * 删除段落
     *
     * @param context
     * @param docId
     * @param paragraphId
     * @return
     */
    public static boolean deleteParagraph(Context context, int docId, int paragraphId, boolean isArticle) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("docid", String.valueOf(docId));
        parameters.put("id", String.valueOf(paragraphId));
        if (isArticle) {
            RequestResult result = HttpUtil.get("/MobileJson/Article/delSection", parameters);
            return validResult(context, result);
        } else {
            RequestResult result = HttpUtil.get("/MobileJson/Index/delSection", parameters);
            return validResult(context, result);
        }
    }

    /**
     * 回答点赞
     *
     * @param context
     * @param answerId
     * @return
     */
    public static int answerLike(Context context, int answerId) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("respondid", String.valueOf(answerId));
        RequestResult result = HttpUtil.get("/MobileJson/Index/answerLike", parameters);
        LinkedTreeMap map = ParseResultFromInfo(context, result, LinkedTreeMap.class);
        if (map == null) {
            return -1;
        } else {
            return Utils.getIntValue(map.get("likecou"));
        }
    }

    /**
     * 回答采纳
     *
     * @param context
     * @param answerId
     * @return
     */
    public static boolean answerAccept(Context context, int answerId) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("respondid", String.valueOf(answerId));
        RequestResult result = HttpUtil.get("/MobileJson/Index/answerAdopt", parameters);
        return validResult(context, result);
    }

    /**
     * 发送咨询
     *
     * @param context
     * @param info
     * @return
     */
    public static boolean sendAdvice(Context context, AdviceInfo info, String content) {
//        用户id:uid，token,咨询类型（1语音、0文字）:asktype,语音地址或文字：question、专家id:expertid
        Map<String, String> parameters = getCommonParameters();
        parameters.put("asktype", String.valueOf(info.getType()));
        parameters.put("question", content);
        parameters.put("expertid", String.valueOf(info.getExpertId()));
        RequestResult result = HttpUtil.get("/MobileJson/Index/Addexpertask", parameters);
        return validResult(context, result);
    }

    /**
     * 发送咨询回答
     *
     * @param context
     * @param item
     * @param adviceId
     * @return
     */
    public static boolean sendAnswer(Context context, AdviceItem item, int adviceId, String times) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("content", item.getContent());
        parameters.put("times", times);
        parameters.put("consultid", String.valueOf(adviceId));
        RequestResult result = HttpUtil.get("/MobileJson/Index/Addexpertanswer", parameters);
        return validResult(context, result);
    }

    /**
     * 获取咨询详情
     *
     * @param context
     * @param adviceId
     * @return
     */
    public static LinkedTreeMap adviceDetail(Context context, int adviceId) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("consultid", String.valueOf(adviceId));
        RequestResult result = HttpUtil.get("/MobileJson/Index/expertaskdetail", parameters);
        return ParseResultFromInfo(context, result, LinkedTreeMap.class);
    }

    /**
     * @param context
     * @return
     */
    public static boolean getVideoPlay(Context context, int videoId) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("videoid", String.valueOf(videoId));
        RequestResult result = HttpUtil.get("/MobileJson/Index/Videoplay", parameters);
        return validResult(context, result);
    }

    public static LinkedTreeMap getMemberPrice(Context context, int videoId) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("videoid", String.valueOf(videoId));
        RequestResult result = HttpUtil.get("/MobileJson/Index/MemberPrice", parameters);
        return ParseResultFromInfo(context, result, LinkedTreeMap.class);
    }

    public static ArrayList getReplyCommentList(Context context, int commentId) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("commentid", String.valueOf(commentId));
        RequestResult result = HttpUtil.get("/MobileJson/Index/ReplyCommentList", parameters);
        return ParseResultFromList(context, result);
    }

    public static ArrayList getExpertArticleList(Context context, int expertId) {
//        http://test.wldsart.com/MobileJson/Index/expertdocumentList
        Map<String, String> parameters = getCommonParameters();
        parameters.put("expertid", String.valueOf(expertId));
        RequestResult result = HttpUtil.get("/MobileJson/Index/expertdocumentList", parameters);
        return ParseResultFromList(context, result);
    }

    public static int videoShare(Context context, int videoId) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("videoid", String.valueOf(videoId));
        RequestResult result = HttpUtil.get("/MobileJson/Index/VideoShare", parameters);
        LinkedTreeMap map = ParseResultFromInfo(context, result, LinkedTreeMap.class);
        if (map != null) {
            return Utils.getIntValue(map.get("coun"));
        }
        return -1;
    }

    public static boolean sendComment(Context context, int number, String commentType, String content) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("number", String.valueOf(number));
        parameters.put("commenttype", commentType);
        parameters.put("commentcontent", content);
        RequestResult result = HttpUtil.get("/MobileJson/Index/Comment", parameters);
        return validResult(context, result);
    }

    public static boolean replyComment(Context context, int commentId, String content) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("commentid", String.valueOf(commentId));
        parameters.put("replycontent", content);
        RequestResult result = HttpUtil.get("/MobileJson/Index/Reply", parameters);
        return validResult(context, result);
    }

    public static ArrayList getRelatedArticle(Context context, int articleId) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("articleid", String.valueOf(articleId));
        RequestResult result = HttpUtil.get("/MobileJson/Index/RelatedDocumentList", parameters);
        return ParseResultFromList(context, result);
    }

    public static LinkedTreeMap getDocumentDetail(Context context, int articleId) {
//        文章id：articleid，标题：title、正文：list、作者昵称:nickname、头像:avatar、文章日期:systime、作者关注:attentionflag、是否点赞：likeflag、是否收藏：collectflag、点赞数：doclike、收藏数：collect、分享数：docshare
        Map<String, String> parameters = getCommonParameters();
        parameters.put("articleid", String.valueOf(articleId));
        RequestResult result = HttpUtil.get("/MobileJson/Index/DocumentDetail", parameters);
        return ParseResultFromInfo(context, result, LinkedTreeMap.class);
    }

    public static boolean goComplain(Context context) {
        Map<String, String> parameters = getCommonParameters();
        RequestResult result = HttpUtil.get("/MobileJson/Index/goComplain", parameters);
        return validResult(context, result);
    }


    /**
     * @param context
     * @return
     */
    public static boolean sendComplain(Context context, int numberId, String type) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("number", String.valueOf(numberId));
        parameters.put("type", type);
        RequestResult result = HttpUtil.get("/MobileJson/Index/SubmitComplain", parameters);
        return validResult(context, result);
    }

    public static int isDone(Context context) {
        Map<String, String> parameters = getCommonParameters();
        RequestResult result = HttpUtil.get("/MobileJson/Index/isdone", parameters);
        if (result.getCode().equals("ok")) {
            return 0;
        } else if (result.getCode().equals("list")) {
            return 1;
        } else {
            validResult(context, result);
            return -1;
        }
    }

    /**
     * 申请专家
     *
     * @param context
     * @param appointTime
     * @return
     */
    public static boolean applyExpert(Context context, String appointTime) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("appointtime", appointTime);
        RequestResult result = HttpUtil.get("/MobileJson/Index/applyexperttime", parameters);
        return validResult(context, result);
    }

    public static boolean updateApplyTime(Context context, int id, String appointTime) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("id", String.valueOf(id));
        parameters.put("appointtime", appointTime);
        RequestResult result = HttpUtil.get("/MobileJson/Index/updateapplyexperttime", parameters);
        return validResult(context, result);
    }

    public static LinkedTreeMap getExpertIntroduction(Context context, int expertId) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("expertid", String.valueOf(expertId));
        RequestResult result = HttpUtil.get("/MobileJson/Index/expertintroduction", parameters);
        return ParseResultFromInfo(context, result, LinkedTreeMap.class);
    }

    /**
     * 咨询支付
     *
     * @param context
     * @param adviceId
     * @param payType
     * @return
     */
    public static String payCharge(Context context, int adviceId, String payType) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("consultid", String.valueOf(adviceId));
        parameters.put("paytype", payType);
        RequestResult result = HttpUtil.get("/MobileJson/Index/paycharge", parameters);
        if ("payorder".equals(result.getCode())) {
            return result.getInfo();
        } else {
            return null;
        }
    }

    public static LinkedTreeMap expertComplain(Context context, int adviceId) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("consultid", String.valueOf(adviceId));
        RequestResult result = HttpUtil.get("/MobileJson/Index/expertcomplain", parameters);
        return ParseResultFromInfo(context, result, LinkedTreeMap.class);
    }

    public static boolean doExpertComplain(Context context, int adviceId, String content, int expertId) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("consultid", String.valueOf(adviceId));
        parameters.put("content", content);
        parameters.put("expertid", String.valueOf(expertId));
        RequestResult result = HttpUtil.get("/MobileJson/Index/doexpertcomplain", parameters);
        return validResult(context, result);
    }

    public static String getActive(Context context) {
        return "http://domi.zhonghouqianyuan.com/MobileJson/Index/jfyh";
//        Map<String, String> parameters = getCommonParameters();
//        RequestResult result = HttpUtil.get("/MobileJson/Index/Activity", parameters);
//        return ParseResultFromInfo(context, result, LinkedTreeMap.class);
    }

    public static boolean allowPlay(Context context, int adviceId) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("consultid", String.valueOf(adviceId));
        RequestResult result = HttpUtil.get("/MobileJson/Index/Answerplay", parameters);
        return validResult(context, result);
    }

    public static ArrayList getMyVideo(Context context, int page) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("page", String.valueOf(page));
        RequestResult result = HttpUtil.get("/MobileJson/Index/myvideo", parameters);
        return ParseResultFromList(context, result);
    }

    /**
     * 余额 balance 手续费 sxf
     *
     * @param context
     * @return
     */
    public static LinkedTreeMap goTixian(Context context) {
        Map<String, String> parameters = getCommonParameters();
        RequestResult result = HttpUtil.get("/MobileJson/Index/goTixian", parameters);
        return ParseResultFromInfo(context, result, LinkedTreeMap.class);
    }

    public static boolean doTixian(Context context, double amount, double price, String cardNo, String bankName, String realName, String phone, String code) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("amount", String.valueOf(amount));
        parameters.put("price", String.valueOf(price));
        parameters.put("cardnumber", cardNo);
        parameters.put("bankname", bankName);
        parameters.put("realname", realName);
        parameters.put("phone", phone);
        parameters.put("code", code);
        RequestResult result = HttpUtil.get("/MobileJson/Index/doTixian", parameters);
        return validResult(context, result);
    }

    /**
     * 我的问题列表
     *
     * @param context
     * @param page
     * @return
     */
    public static ArrayList myExpertAnswer(Context context, int page) {
//        专家姓名expertname、问题描述question、状态status、咨询id：consultid、
// 专家id：expertid、提问人id：uid、systime:咨询时间
        Map<String, String> parameters = getCommonParameters();
        parameters.put("page", String.valueOf(page));
        RequestResult result = HttpUtil.get("/MobileJson/Index/myExpertanswer", parameters);
        return ParseResultFromList(context, result);
    }

    /**
     * 获取我的咨询列表
     *
     * @param context
     * @param page
     * @return
     */
    public static ArrayList myExpertAsk(Context context, int page) {
//        专家姓名expertname、问题描述question、状态status、咨询id：consultid、
// 专家id：expertid、提问人id：uid、systime:咨询时间
        Map<String, String> parameters = getCommonParameters();
        parameters.put("page", String.valueOf(page));
        RequestResult result = HttpUtil.get("/MobileJson/Index/myExpertask", parameters);
        return ParseResultFromList(context, result);
    }

    /**
     * 获取我的粉丝列表
     *
     * @param context
     * @return
     */
    public static ArrayList getMyFans(Context context, int uid) {
//        uid：用户id,nickname:昵称，avatar:头像，company：公司,expertflag:是否专家,job:职位数据,gzflag:是否关注
        Map<String, String> parameters = new HashMap<String, String>();
        parameters.put("uid", String.valueOf(uid));
        RequestResult result = HttpUtil.get("/MobileJson/Index/myFans", parameters);
        return ParseResultFromList(context, result);
    }

    /**
     * 获取我的关注列表
     *
     * @param context
     * @return
     */
    public static ArrayList getMyFollow(Context context, int uid) {
        //        uid：用户id,nickname:昵称，avatar:头像，company：公司,expertflag:是否专家,job:职位数据,gzflag:是否关注
        Map<String, String> parameters = new HashMap<String, String>();
        parameters.put("uid", String.valueOf(uid));
        RequestResult result = HttpUtil.get("/MobileJson/Index/myFollow", parameters);
        return ParseResultFromList(context, result);
    }

    /**
     * 添加关注
     *
     * @param context
     * @param uid
     * @return
     */
    public static boolean addAttention(Context context, int uid) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("attentionuid", String.valueOf(uid));
        RequestResult result = HttpUtil.get("/MobileJson/Index/addAttention", parameters);
        return validResult(context, result);
    }

    /**
     * 取消关注
     *
     * @param context
     * @param uid
     * @return
     */
    public static boolean unAttention(Context context, int uid) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("attentionuid", String.valueOf(uid));
        RequestResult result = HttpUtil.get("/MobileJson/Index/unAttention", parameters);
        return validResult(context, result);
    }

    /**
     * 我的草稿箱
     *
     * @param context
     * @param page
     * @return
     */
    public static ArrayList myDraftBox(Context context, int page) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("page", String.valueOf(page));
        RequestResult result = HttpUtil.get("/MobileJson/Index/myDraftbox", parameters);
        return ParseResultFromList(context, result);
    }

    /**
     * 删除草稿
     *
     * @param context
     * @param articleId
     * @return
     */
    public static boolean delDraft(Context context, int articleId) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("articleid", String.valueOf(articleId));
        RequestResult result = HttpUtil.get("/MobileJson/Index/delDraft", parameters);
        return validResult(context, result);
    }

    /**
     * 意见反馈
     *
     * @param context
     * @param contact
     * @param content
     * @return
     */
    public static boolean sendSuggest(Context context, String contact, String content) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("contact", contact);
        parameters.put("content", content);
        RequestResult result = HttpUtil.get("/MobileJson/Index/yjAdd", parameters);
        return validResult(context, result);
    }

    /**
     * 参与悬赏资格校验
     *
     * @param context
     * @param questionId
     * @return
     */
    public static boolean partakecheck(Context context, int questionId) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("questionid", String.valueOf(questionId));
        RequestResult result = HttpUtil.get("/MobileJson/Index/partakecheck", parameters);
        return validResult(context, result);
    }

    /**
     * 参与悬赏
     *
     * @param context
     * @param questionId
     * @param content
     * @return
     */
    public static boolean partakeanswer(Context context, int questionId, String content) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("questionid", String.valueOf(questionId));
        parameters.put("respondcontent", content);
        RequestResult result = HttpUtil.get("/MobileJson/Index/partakeanswer", parameters);
        return validResult(context, result);
    }

    /**
     * 文章分享
     *
     * @param context
     * @param articleId
     * @return
     */
    public static int docShare(Context context, int articleId) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("articleid", String.valueOf(articleId));
        RequestResult result = HttpUtil.get("/MobileJson/Index/docshare", parameters);
        LinkedTreeMap map = ParseResultFromInfo(context, result, LinkedTreeMap.class);
        if (map == null)
            return -1;
        return Utils.getIntValue(map.get("coun"));
    }

    public static int docLike(Context context, int articleId) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("articleid", String.valueOf(articleId));
        RequestResult result = HttpUtil.get("/MobileJson/Index/DocLike", parameters);
        LinkedTreeMap map = ParseResultFromInfo(context, result, LinkedTreeMap.class);
        if (map == null)
            return -1;
        return Utils.getIntValue(map.get("coun"));
    }

    /**
     * 排行榜
     *
     * @param context
     * @param type    v:视频,w:文章,e：专家,其他直接传文字
     * @return
     */
    public static ArrayList getRank(Context context, String type) {
//        code:ok msg:成功，
// /*视频 rowno:排名，videoid：视频id，videoname：视频名称，cover：封面，videopath：视频地址，nickname：上传会员昵称，play：播放次数，
// /*文章 rowno:排名，articleid:文章id,title:文章标题,lable:文章简介，nickname：上传会员昵称，click：点击次数，
// /*专家 rowno：排名，uid：用户id，nickname：会员昵称，avatar：头像，realname：真实姓名，expertdomain：专家领域
// /*董秘 rowno：排名，uid：用户id，nickname：会员昵称，avatar：头像，realname：真实姓名，expertdomain：领域
        Map<String, String> parameters = getCommonParameters();
        parameters.put("type", type);
        RequestResult result = HttpUtil.get("/MobileJson/Index/paihangbang", parameters);
        return ParseResultFromList(context, result);
    }

    /**
     * 搜索
     *
     * @param context
     * @param type    v:视频,w:文章,other:其他,全部:all
     * @return
     */
    public static ArrayList search(Context context, String content, String type) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("type", type);
        parameters.put("keyword", content);
        RequestResult result = HttpUtil.get("/MobileJson/Index/search", parameters);
        return ParseResultFromList(context, result);
    }

    public static LinkedTreeMap getParagraphList(Context context, int articleId) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("docid", String.valueOf(articleId));
        RequestResult result = HttpUtil.get("/MobileJson/Article/docDetail", parameters);
        return ParseResultFromInfo(context, result, LinkedTreeMap.class);
    }

    public static LinkedTreeMap getExpertDetail(Context context, int expertId) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("expertid", String.valueOf(expertId));
        RequestResult result = HttpUtil.get("/MobileJson/Index/expertdetail", parameters);
        return ParseResultFromInfo(context, result, LinkedTreeMap.class);
    }

    public static ArrayList getExpertDomainList(Context context) {
        Map<String, String> parameters = getCommonParameters();
        RequestResult result = HttpUtil.get("/MobileJson/Index/expertdomainList", parameters);
        return ParseResultFromList(context, result);
    }

    public static LinkedTreeMap payVideoByScore(Context context, int videoId) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("videoid", String.valueOf(videoId));
        RequestResult result = HttpUtil.get("/MobileJson/Index/payVideoByIntegral", parameters);
        return ParseResultFromInfo(context, result, LinkedTreeMap.class);
    }

    public static String buyCourse(Context context, int videoId, String payType, String type) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("videoid", String.valueOf(videoId));
        parameters.put("buytype", payType);
        parameters.put("type", type);
        RequestResult result = HttpUtil.get("/MobileJson/Index/payVideoByMoney", parameters);
        if (validResult(context, result)) {
            return result.getInfo();
        } else {
            return null;
        }
//        return ParseResultFromInfo(context, result, LinkedTreeMap.class);
    }

    public static ArrayList hotSearch(Context context) {
        Map<String, String> parameters = getCommonParameters();
        RequestResult result = HttpUtil.get("/MobileJson/Index/HotSearch", parameters);
        return ParseResultFromList(context, result);
    }

    /**
     * 获取悬赏文章段落
     *
     * @param context
     * @param questionId
     * @return
     */
    public static ArrayList rewardParagraphDetail(Context context, int questionId) {
        Map<String, String> parameters = getCommonParameters();
        parameters.put("questionid", String.valueOf(questionId));
        RequestResult result = HttpUtil.get("/MobileJson/Index/AskDocDetail", parameters);
        LinkedTreeMap map = ParseResultFromInfo(context, result, LinkedTreeMap.class);
        if (map == null) {
            return new ArrayList();
        }
        return (ArrayList) map.get("listcontent");
    }

    /**
     * 获取标签
     *
     * @param context
     * @return
     */
    public static LinkedTreeMap getLabel(Context context) {
        Map<String, String> parameters = getCommonParameters();
        RequestResult result = HttpUtil.get("/MobileJson/Index/getLabel", parameters);
        return ParseResultFromInfo(context, result, LinkedTreeMap.class);
    }

    private static Map<String, String> getCommonParameters() {
        Map<String, String> parameters = new HashMap<String, String>();
        parameters.put("uid", String.valueOf(User.getCurrentUser().getUid()));
        parameters.put("token", User.getCurrentUser().getToken());
        return parameters;
    }

    public static String getShareUrl() {
        return String.format(Locale.CHINESE, "http://domi.zhonghouqianyuan.com/MobileJson/Index/ShareView", HttpUtil.ServerAddress);
    }

    public static <T> T ParseResultFromInfo(Context context, RequestResult result, Type typeOfT) {
        if (validResult(context, result)) {
            return HttpUtil.fromJson(result.getInfo(), typeOfT);
        }
        return null;
    }

    public static ArrayList ParseResultFromList(Context context, RequestResult result) {
        if (validResult(context, result)) {
            return result.getList();
        }
        return new ArrayList();
    }

    public static boolean validResult(final Context context, final RequestResult result) {
        if ("ok".equals(result.getCode())) {
            return true;
        } else {
            final Activity activity = (Activity) context;
            if (activity == null || activity.isDestroyed()) {
                return false;
            }
            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // token失效时返回登录界面
                    if ("e0001".equals(result.getCode())) {
                        HomeActivity.exit(activity);
                    } else {
                        Toast.makeText(context, result.getMsg(), Toast.LENGTH_SHORT).show();
                    }
                }
            });
            return false;
        }
    }
}
